from django.conf import settings

from order.models import OrderProduct
from shop.models import Product


class Order(object):

    def __init__(self, request):

        self.session = request.session
        self.cart = self.session.get(settings.CART_SESSION_ID)


    def create(self, created_order):

        """ Создаем заказ """

        product_ids = list(map(int, list(self.cart.keys())))
        print(f'keys - {product_ids}')
        products = Product.objects.filter(id__in=product_ids)

        for product in products:
            OrderProduct.objects.create(order=created_order, product=product)

        self.session.flush()
